<?php
class sw_scgame
{
	private $array;
	private $localpath;
	private $templatepath;
	private $startid;
	private $endid;
	private $gamemodel;
	private $servermodel;
	private $tjmodel;
	private $ftparr;
	private $picdomain;
	
	public function __construct($startid,$endid=1)
	{
		
		$this->startid=$startid;
		$this->endid=$endid;
		$this->ftparr=get_ftp("shunwang");
		$this->gamemodel=Game::model();
		$this->servermodel=Server::model();
		$this->tjmodel=Tj::model();
		$this->templatepath = 'tpl/shunwang/game.html';
		if(func_num_args()>1) $this->cycle_schtml();
		else $this->single();
	}
	private function single(){
	    $sql="select id from {{game}} where id='".$this->startid."'";
        $dataarr =$this->gamemodel->findBySql($sql);
        $this->getarray($dataarr['id']);		   
		$this->localpath="../schtml/shunwang/game/".$dataarr['id'].".html";	
	    $ftpObj=new Syncftp($this->localpath,$this->templatepath,$this->array,$this->ftparr['shunwang_html']);
	    $ftpObj->upload();
	    new sw_scserver($this->startid);
	    new sw_scinclude($this->startid);
	}
	private function cycle_schtml(){
		$id=isset($_GET['sid']) ? $_GET['sid'] : 0;		
		if($this->startid=="all" && $this->endid=="all") $where="";
		else if($this->startid=="all" && $this->endid!="all") $where="where id<='".$this->endid."'";
		else if($this->startid!="all" && $this->endid=="all") $where="where id>='".$this->startid."'";
		else $where="where id>='".$this->startid."' and id<='".$this->endid."'";	
		$sql="select id from {{game}} ".$where;	
	    $idrange = Yii::app()->shunwang->createCommand($sql)->queryAll();
		$count=count($idrange);
		if($count==0) exit("<script type='text/javascript'>alert('该id不存在！');</script>");
		$cycleid=($id+10)>$count ? $count : ($id+10);
		for($i=$id;$i<$cycleid;$i++){
			$this->getarray($idrange[$i]['id']);
			$this->localpath="../schtml/shunwang/game/".$idrange[$i]['id'].".html";
			new sw_scserver($idrange[$i]['id']);
			new sw_scinclude($idrange[$i]['id']);
			$ftpObj=new Syncftp($this->localpath,$this->templatepath,$this->array,$this->ftparr['shunwang_html']);
			$ftpObj->upload();
		}
		
		if(($id+10)>$count) exit("<script type='text/javascript'>alert('生成成功');</script>");

		echo "正在生成第<span style='color:#F00'> ".$id." </span> -- <span style='color:#F00'> ".$cycleid." </span>条数据，还剩<span style='color:#F00'> ".($count-$cycleid)." </span>条数据，总共<span style='color:#F00'> ".$count." </span>条数据";

		exit("<script type='text/javascript'>location.href='?startid=".$this->startid."&endid=".$this->endid."&sid=".($id+10)."';</script>");
	}
	private function getarray($id)
	{
		$this->array=array();
		$gameinfo=$this->gamemodel->findByPk($id);          
		$this->array['server']="javascript:void(0)";
		$this->array['script']='<script type="text/javascript">var aData={pt:'.$gameinfo['pid'].',gameid:'.$id.',logo:"'.Yii::app()->params['domin'].$gameinfo['logo'].'",weburl:"'.$gameinfo['weburl'].'",payurl:"'.$gameinfo['payurl'].'",bbsurl:"'.$gameinfo['bbsurl'].'",faqurl:"'.$gameinfo['faqurl'].'"};</script>';
		$picarr=unserialize($gameinfo->data);
		$listarr=array();
		$serverarr=$this->servermodel->findAllBySql("select name,state,sid from {{server}} where tj=1 and gid=".$id." order by addtime desc limit 4");
		foreach ($serverarr as $k => $v) {
			$listarr[]=array("name"=>$v['name'],"state"=>$v['state'],"sid"=>$v['sid']);
		}
		if(count($listarr)<4){
		    $serverotherarr=$this->servermodel->findAllBySql("select name,state,sid from {{server}} where tj!=1 and gid=".$id." order by addtime desc limit ".(4-count($listarr))."");  
		    foreach ($serverotherarr as $_v) {
			    $listarr[]=array("name"=>$_v['name'],"state"=>$_v['state'],"sid"=>$_v['sid']);
		    }
		}
		foreach ($picarr as $key => $value) {
			if($id==9){
			    $source=file_get_contents("http://access.cs175.cn/ls");
		        preg_match_all('%<tr[^>]+data[^>]+>[^<]+<td>(.*)</td>[^<]+<td>(.*)</td>[^<]+<td>.*</td>[^<]+<td>.*</td>[^<]+</tr>%isU',$source,$matcharr);
		        $this->array['gamelist'].='<li '.($key==3 ? 'style="margin-right:0;"' : '').'><div class="info"><div class="tag "><i class="btns flag_recom"></i><span class="title eps" title="'.$matcharr[2][$key].'">'.$matcharr[2][$key].'</span><b class="s1"></b></div><div class="enter"><a href="javascript:void(0)" class="btn_enter" ip="'.$matcharr[1][$key].'" id="'.$gameinfo['gid'].'" type="cs">进入游戏</a></div></div><div class="card_cover"></div><div class="btns shadow"></div><div class="card"><img src="'.Yii::app()->params['picdomin'].$value['img'].'"/></div></li>';
		    }elseif($id==10){
		    	$jsonarr=json_decode(file_get_contents("http://www.l4dol.com/ls/json"),true);
		        $this->array['gamelist'].='<li '.($key==3 ? 'style="margin-right:0;"' : '').'><div class="info"><div class="tag "><i class="btns flag_recom"></i><span class="title eps" title="'.$jsonarr[$key]['HostName'].'">'.$jsonarr[$key]['HostName'].'</span><b class="s1"></b></div><div class="enter"><a href="javascript:void(0)" class="btn_enter" ip="'.$jsonarr[$key]['ServerIp'].'" id="'.$gameinfo['gid'].'" type="qs">进入游戏</a></div></div><div class="card_cover"></div><div class="btns shadow"></div><div class="card"><img src="'.Yii::app()->params['picdomin'].$value['img'].'"/></div></li>';
		    }else{
			    $this->array['gamelist'].='<li '.($key==3 ? 'style="margin-right:0;"' : '').'><div class="info"><div class="tag "><i class="btns flag_recom"></i><span class="title eps" title="'.$listarr[$key]['name'].'">'.$listarr[$key]['name'].'</span><b class="s'.$listarr[$key]['state'].'"></b></div><div class="enter"><a href="javascript:void(0)" class="btn_enter" id="'.$gameinfo['gid'].'-'.$listarr[$key]['sid'].'">进入游戏</a></div></div><div class="card_cover"></div><div class="btns shadow"></div><div class="card"><img src="'.Yii::app()->params['picdomin'].$value['img'].'"/></div></li>';
			}
		}
		$tjtxtarr=$this->tjmodel->findAllBySql("select url,title,addtime from {{tj}} where gid=".$id." and typeid=3 order by addtime desc limit 5");
		foreach ($tjtxtarr as $key => $value) {
			$this->array['gametxt'].='<li><span class="eps">'.date("m-d",$value['addtime']).'</span><a href="'.$value['url'].'" target="_blank">'.$value['title'].'</a></li>';
		}

		$tjpicarr=$this->tjmodel->findAllBySql("select url,title,pic from {{tj}} where gid=".$id." and typeid=2 order by addtime desc limit 5");

		foreach ($tjpicarr as $key => $value) {
			$this->array['gamepic'].='<li><a href="'.$value['url'].'" title="'.$value['title'].'" target="_blank"><img src="'.Yii::app()->params['picdomin'].$value['pic'].'" /></a></li>';
		}
	}
}